package com.apes.fn.server.workDesk.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.server.workDesk.model.WorkDeskUserCount;
import com.apes.fn.server.workDesk.repository.WorkDeskConfigRepository;
import com.apes.fn.server.workDesk.repository.WorkDeskUserCountRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.rbac.repository.UserRepository;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

@Service("workDeskUseCountService")
public class WorkDeskUseCountService extends DomainService {

    @Autowired
    private WorkDeskUserCountRepository workDeskUserCountRepository;

    @Autowired
    private WorkDeskConfigRepository workDeskConfigRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 统计使用的用户
     * @param req
     * @return
     */
    public Map countUse(SimpleRequest req){
        JSONObject jo = req.getJO();
        try {
            String workDeskConfigId = (String)req.getJO().get("id");
            String userId = req.getSession().getUserId();
            if (workDeskConfigId == null) throw new RuntimeException("缺少id");
            WorkDeskUserCount workDeskUserCount = null;
            if((workDeskUserCount = findByUseDateAndUseParty(userId,workDeskConfigId)) == null){
                workDeskUserCount = initWorkDeskUserCount(workDeskConfigId,jo,userId);
            }
            workDeskUserCountRepository.save(workDeskUserCount);
        } catch (RuntimeException e) {
            e.printStackTrace();
            Document document = new Document();
            document.put("request", jo);
            document.put("message", e.getMessage());
            document.put("date",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            mongoTemplate.getCollection("WorkDeskUseCountException").insertOne(document);
            return MapUtil.mapper("success","false");
        }
        return MapUtil.mapper("success","true");
    }

    /**
     * 初始化用户使用统计
     * @param workDeskConfigId
     * @param jo
     * @param userId
     * @return
     */
    public WorkDeskUserCount initWorkDeskUserCount(String workDeskConfigId,JSONObject jo,String userId){
        WorkDeskUserCount workDeskUserCount = new WorkDeskUserCount();
        workDeskUserCount.setUseDate(new Date());
        workDeskUserCount.setUseCount(1);
        workDeskUserCount.setUseWorkDeskFunction(workDeskConfigRepository.findById(workDeskConfigId).get());
        workDeskUserCount.setUseUser(userRepository.findById(Integer.parseInt(userId)).get());
        return workDeskUserCount;
    }


    public WorkDeskUserCount findByUseDateAndUseParty(String userId,String workDeskConfigId){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        WorkDeskUserCount workDeskUserCount = workDeskUserCountRepository.findByUseDateAndUsePartyAndUseWorkDeskFunction(format.format(new Date()), Integer.parseInt(userId),workDeskConfigId);
        if (workDeskUserCount == null) return null;
        workDeskUserCount.setUseCount(workDeskUserCount.getUseCount() + 1);
        return workDeskUserCount;
    }

}
